package com.file.system.controller.system;


import com.file.system.entity.res.CommonResult;
import com.file.system.entity.system.WebFile;
import com.file.system.plugin.file.FileTool;
import com.file.system.service.system.IWebFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;


/**
 * 系统文件
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/web/file")
public class WebFileController {

    @Autowired
    private IWebFileService service;
    @Autowired
    private FileTool fileTool;


    /**
     * 下载文件
     *
     * @param id 文件id
     * @return CommonResult<WebFile>
     * @apiNote -
     * @author jch
     */
    @GetMapping("/download/{id}")
    public void download(@PathVariable
                         @Min(value = 1, message = "webFileId长度最小1")
                         Long id,
                         HttpServletResponse response) {
        fileTool.download(id, response);
    }


    /**
     * 通过id删除系统文件
     *
     * @param id 文件id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable
                            @Min(value = 1, message = "id长度最小1")
                            Long id) {
        return fileTool.delete(id);
    }


    /**
     * 上传文件对应数据
     *
     * @param file 文件流
     * @return CommonResult<WebFile>
     * @apiNote -
     * @author jch
     */
    @PostMapping("/upload/{fkObjectId}")
    public CommonResult<WebFile> uploadObject(
            @RequestParam("file") MultipartFile file,
            @PathVariable
            @Min(value = 1, message = "fkObjectId长度最小1")
            Long fkObjectId) {
        // 调用fileTool的upload方法，上传文件，并返回文件id
        return fileTool.upload(file, fkObjectId);
    }
}

